package com.zjw.zy.base;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavBackStackEntry;

import com.zjw.zy.R;
import com.zjw.zy.ext.BaseFragmentExtKt;
import com.zjw.zy.manager.PageManager;
import com.zjw.zy.utils.AppUtils;
import com.zjw.zy.utils.BackHelper;
import com.zjw.zy.utils.LogUtils;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/26 9:10
 */
public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";

    private View mRootView;

    private boolean isFirstCreate = false;

    protected boolean isPageVisible = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        isPageVisible = true;
        onPageEnter();
    }

    @Override
    public void onPause() {
        super.onPause();
        isPageVisible = false;
        onPageExit();
    }

    protected <V extends View> V findViewById(@IdRes int id) {
        if (mRootView == null) {
            throw new RuntimeException("mRootView == null");
        }

        return mRootView.findViewById(id);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!isFirstCreate) {
            isFirstCreate = true;
            onFindView(mRootView);
            onBindViewValue();
            onBindViewListener();
            onPageCreate();
            onSubscribe();
        } else {
            onViewRestore(savedInstanceState);
        }
    }

    protected void onPageCreate() {
    }

    protected abstract void onPageEnter();

    protected abstract void onViewRestore(@Nullable Bundle savedInstanceState);

    protected abstract void onSubscribe();

    protected abstract void onBindViewListener();

    protected abstract void onBindViewValue();

    protected abstract void onFindView(View rootView);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: ");

        if (mRootView == null) {
            mRootView = inflater.inflate(onLayoutId(), container, false);
        }

        return mRootView;
    }

    protected abstract int onLayoutId();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        onPageDestroy();
    }

    protected abstract void onPageExit();


    @Override
    public void onDestroy() {
        super.onDestroy();

        onPageDestroy();
    }

    protected void onPageDestroy() {
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    public void onBackClick() {
        LogUtils.d(TAG, "onBackClick");
        NavBackStackEntry entry = PageManager.getBackStackLastEntry();

        if (entry == null || entry.getDestination().getId() == R.id.nav_start_up_fragment) {
            PageManager.goMain();
            return;
        }
        BaseFragmentExtKt.back(this);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        AppUtils.getMainActivity().startActivityForResult(intent, requestCode);
    }


    /**
     * 不再finish，保持长活
     */
    protected void onCallMoveToBack() {
        LogUtils.d(TAG, "onBackPressed");
        //if (BackHelper.canExit()) {
        requireActivity().moveTaskToBack(true);
        //    return;
        //}
        //BackHelper.markBackPressed();
    }
}
